package core.pagination;

public class BasicCommand implements Command {
	private Action action;
	private ExceptionHandler handler;

	public BasicCommand(Action action) {
		if (action == null) {
			throw new IllegalArgumentException("Action must not be null.");
		}
		this.action = action;
		this.handler = (ExceptionHandler) this.action;
	}

	@Override
	public Command onException(ExceptionHandler handler) {
		if (handler != null) {
			this.handler = handler;
		}
		return this;
	}

	public Result run() throws Exception {
		Result result = null;
		try {
			result = action.execute();
		} catch (Exception e) {
			handler.handleException(e);
		}
		return result;
	}

}